package ca.etsmtl.log430.lab3.presentation;

import java.util.ArrayList;
import java.util.Collection;

import ca.etsmtl.log430.lab3.gestion.CourseType;
import ca.etsmtl.log430.lab3.gestion.Teacher;
import ca.etsmtl.log430.lab3.gestion.ifaces.ICommonData;
import ca.etsmtl.log430.lab3.gestion.ifaces.ICourse;
import ca.etsmtl.log430.lab3.presentation.cmdline.Termio;
public class Menus {

	/**
	 * @uml.property  name="controller"
	 * @uml.associationEnd  
	 */
	ICommonData controller;
	
	
	public Menus(ICommonData controller) {
		super();
		this.controller = controller;
	}

	public Menus() {
		// TODO Auto-generated constructor stub
	}

	public char mainMenu() {

		
		
		Termio terminal = new Termio();
		char userChoice = ' ';
		boolean error = true;

		while (error) {

			System.out.println("\n\n1) List Teachers");
			System.out.println("2) List Courses");
			System.out.println("3) List courses currently assigned to a teacher this term");
			System.out.println("4) List teacher(s) currently assigned to a course this term");
			System.out.println("5) Assign a teacher to a course");
			System.out.println("6)  liste des cours qu'un enseignant a d�j� enseign�s avant la pr�sente session");
			System.out.println("7)  Affiche une liste de cours qui n'ont pas �t� attribu�");
			System.out.println("X) Exit");
			System.out.print("\n\nEnter your choice and press return >> ");

			userChoice = terminal.keyboardReadChar();
			
			ArrayList<Character> choicesList = new ArrayList<Character>();
			char[] choices = {'X','x','1','2','3','4','5','6','7'};
			for (char c : choices) {
				choicesList.add(c);
			}
		
			if(!choicesList.contains(userChoice))
			{

				System.out.print("\n\n*** Invalid Choice:: " + userChoice
						+ " ***");
			} else {

				error = false;

			} // if

		} // while

		return (userChoice);

	} // MainMenu

	public Teacher pickTeacher() {

		Termio terminal = new Termio();
		String userChoice;
		Teacher teacher = null;

		System.out.print("\n\nEnter Teacher ID and press return >> ");
		userChoice = terminal.keyboardReadString();
		
		teacher = controller.findTeacherByID(userChoice);

		if (teacher == null) {

			System.out.println("\n\n*** Teacher ID " + userChoice
					+ " not found ***");

		} // if

		return (teacher);

	} // pickTeacher

	/**
	 * 
	 * @return
	 */
	public boolean sayYesOrNo()
	{
		Termio terminal = new Termio();
		String userChoice;
		
		while(true)
		{
			System.out.print("\n\nEnter Y for yes or N for no and press return >> ");
			userChoice = terminal.keyboardReadString();
			
			if(userChoice.equals("Y"))
				return true;
			if(userChoice.equals("N"))
				return false;
		
			System.err.println("\n\n*** wrong value try again ***");
		}
	
	}
	
	public ICourse pickCourse(Collection<ICourse> courses)
	{
		Termio terminal = new Termio();
		String userChoiceCourseID;
		String userChoiceGroup;
		String userChoiceActivityType;
		//Course course = null;
		ICourse course=null;
		System.out.print("\nEnter Course ID and press return >> ");
		userChoiceCourseID = terminal.keyboardReadString();

		System.out.print("\nEnter group and press return >> ");
		userChoiceGroup = terminal.keyboardReadString();

		System.out.print("\nEnter type of activity (CRS or LAB) and press return >> ");
		userChoiceActivityType = terminal.keyboardReadString();

		CourseType courseType = CourseType.valueOf(userChoiceActivityType);
		
		course = controller.findCourseByIDActivityAndGroup(userChoiceCourseID,
				userChoiceGroup, courseType);

		if (course == null) {
			System.out.print("\n\n*** Course ID:" + userChoiceCourseID);
			System.out.println(" Group:" + userChoiceGroup
					+ " not found ***");

		} // if

		return (course);
		
	}
	
	
	/**
	 *  ASK for Course ID and GROUP
	 * 
	 * @return
	 */
	public ICourse pickCourse() {

		Termio terminal = new Termio();
		String userChoiceCourseID;
		String userChoiceGroup;
		String userChoiceActivityType;
		//Course course = null;
		ICourse course=null;
		System.out.print("\nEnter Course ID and press return >> ");
		userChoiceCourseID = terminal.keyboardReadString();

		System.out.print("\nEnter group and press return >> ");
		userChoiceGroup = terminal.keyboardReadString();

		System.out.print("\nEnter type of activity (CRS or LAB) and press return >> ");
		userChoiceActivityType = terminal.keyboardReadString();

		CourseType courseType = CourseType.valueOf(userChoiceActivityType);
		
		course = controller.findCourseByIDActivityAndGroup(userChoiceCourseID,
				userChoiceGroup, courseType);
		
		if (course == null) {
			System.out.print("\n\n*** Course ID:" + userChoiceCourseID);
			System.out.println(" Group:" + userChoiceGroup
					+ " not found ***");
			
		} // if
		
		return (course);

	} // pickCourse

} // Menus
